using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Nsb.Classes;
using jQueryApi;

namespace MinionsEdit.Classes
{
    [IgnoreNamespace]
    public class MiPropPage : Cluster
    {
        #region Css

        private const string WidgetCss = @"
.MiEPPage {
    display: block;
    postion: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
.MiEPEnder {
    display: block;
    clear: both;
    width: 1px;
    height: 10px;
}
.MiEPBox {
    display: inline-block;
    clear: both;
    width: 120px;
    min-height: 120px;
    vertical-align: top;
}
.MiEPLabel {
    color: lightblue;
    font-weight: bold;
    text-align: center;
    margin: 5px 0 2px 0;
}
        ";

        #endregion Css

        private readonly jQueryObject _container;
        private readonly MiPropPanel _panel;
        private jQueryObject _widget;
        private readonly Dictionary<string, MiProperty> _allProps = new Dictionary();

        static MiPropPage()
        {
            AddStyle(WidgetCss);
        }

        private MiPropPage(MiPropPanel panel, jQueryObject container)
        {
            _panel = panel;
            _container = container;
            Element = jQuery.FromHtml("<div class='MiEPPage'/>")
                .AppendTo(_container)
                .Show()
                ;
            NewBox();
        }

        public string Units
        {
            get { return _panel.Units; }
        }

        public MiPropPage Append(MiProperty prop)
        {
            prop.Initialize(this);
            _widget.Append(prop.Element);
            return this;
        }

        public static MiPropPage Ev(MiPropPanel panel, jQueryObject container)
        {
            jQuery.Select(".MiEPPage,.MiEPEnder", container).Remove();
            return new MiPropPage(panel, container);
        }

        public void QueSave()
        {
            _panel.QueSave = true;
            Evo.Trigger(MinionEdit.MaskRubberbandEv, MinionEdit.ActiveFocus);
        }

        #region Widgets

        public MiPropPage NewBox()
        {
            _widget = jQuery.FromHtml("<div class='MiEPBox'/>")
                .AppendTo(Element)
                ;
            return this;
        }

        public MiPropPage Label(string label)
        {
            jQuery.FromHtml("<div class='MiEPLabel'/>")
                .AppendTo(_widget)
                .Html(label)
                ;
            return this;
        }

        public MiPropPage BoxWidth(int w)
        {
            _widget.CSS(new Dictionary("width", w));
            return this;
        }

        public MiPropPage Ender()
        {
            _container.Append(jQuery.FromHtml("<div class='MiEPEnder'/>"));
            return this;
        }

        public void AddProp(string key, MiProperty value)
        {
            _allProps[key.ToLowerCase()] = value;
        }

        public MiProperty GetProp(string key)
        {
            return _allProps[key.ToLowerCase()];
        }

        #endregion Widgets
    }
}